package com.neu.credit.creditscore.controller;

import com.neu.credit.creditscore.common.utils.Result;
import com.neu.credit.creditscore.dto.req.ParkingPreOrderInfoReq;
import com.neu.credit.creditscore.dto.req.ParkingPreOrderInfoReqTest;
import com.neu.credit.creditscore.dto.res.parking.*;
import com.neu.credit.creditscore.entity.books.Books;
import com.neu.credit.creditscore.entity.parking.ParkingLots;
import com.neu.credit.creditscore.entity.parking.ParkingSpaces;
import com.neu.credit.creditscore.service.parking.ParkingLotsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/park")
//@PreAuthorize("hasAuthority('')")
@Tag(name = "停车场相关接口")
@Slf4j
public class ParkingController {

    @Autowired
    private ParkingLotsService parkingLotsService;

    @GetMapping("/list")
    @Operation(summary = "获取默认推荐的停车场列表")
    public Result<List<ParkingLotsSimpleInfoRes>> getRecommendParking() {
        try {
            return parkingLotsService.getRecommendParking();
        } catch (Exception e) {
            log.error("getRecommendParking has a error", e);
            return Result.error("获取默认推荐的停车场列表失败！");
        }
    }
    @GetMapping("/score")
    @Operation(summary = "获取到当前积分")
    public Result<String> getScore(@RequestParam("id") Integer id) {
        try {
            return parkingLotsService.getScore(id);
        } catch (Exception e) {
            log.error("getRecommendParking has a error", e);
            return Result.error("获取默认推荐的停车场列表失败！");
        }
    }
    @GetMapping("/type/list")
    @Operation(summary = "根据类别的停车场列表")
    public Result<ParkingLotsTypeRes> getParkingByType(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "type", defaultValue = "0") int type,
            @RequestParam(value = "title", defaultValue = "") String title
    ) {
        try {
            return parkingLotsService.getParkingByType(pageNum, pageSize, type, title);
        } catch (Exception e) {
            log.error("getRecommendParking has a error", e);
            return Result.error("获取停车场列表失败！");
        }
    }

    @GetMapping("/spaces/{lotId}")
    @Operation(summary = "获取车位信息")
    public Result<List<ParkingSpaces>> getSpaces(@PathVariable("lotId") int lotId) {
        try {
            return parkingLotsService.getSpaces(lotId);
        } catch (Exception e) {
            log.error("getSpaces has a error", e);
            return Result.error("获取车位信息失败！");
        }
    }

    @GetMapping("/lot/{lotId}")
    @Operation(summary = "获取停车场信息")
    public Result<ParkingLots> getParkingLotInfoById(@PathVariable("lotId") int lotId) {
        try {
            return parkingLotsService.getParkingLotInfoById(lotId);
        } catch (Exception e) {
            log.error("getParkingLotInfoById has a error", e);
            return Result.error("获取停车场信息失败！");
        }
    }
    @PostMapping("/preorder_1/save")
    public Result<ParkingPreOrderInfoRes> savePreorder(@RequestParam("lotId") Integer lotId,
                                                       @RequestParam("spaceNum") String spaceNum,
                                                       @RequestParam("startTime") long startTime,
                                                       @RequestParam("endTime") long endTime,
                                                       @RequestParam("id") Integer id) {
        System.out.println(lotId);
        System.out.println(spaceNum);
        System.out.println(startTime);
        System.out.println(endTime);
        System.out.println(id);
        ParkingPreOrderInfoReq request=new ParkingPreOrderInfoReq(lotId, spaceNum, startTime, endTime);

        try {
            return parkingLotsService.savePreOrderInfo(request, id);
        } catch (Exception e) {
            log.error("savePreOrderInfo has a error", e);
            return Result.error("保存停车预约预下单失败！");
        }
    }


    @PostMapping("/preOrder/save")
    @Operation(summary = "保存（修改）停车预约预下单")
    public Result<ParkingPreOrderInfoRes> savePreOrderInfo(@RequestBody ParkingPreOrderInfoReq req, @RequestParam("id") Integer id, HttpServletRequest request) throws IOException {
        try {
            return parkingLotsService.savePreOrderInfo(req,id);
        } catch (Exception e) {
            log.error("getPreOrderInfo has a error", e);
            return Result.error("保存停车预约预下单失败！");
        }
    }

    @GetMapping("/order/{appointmentId}")
    @Operation(summary = "确认停车预约下单")
    public Result<String> orderParking(@PathVariable("appointmentId") int appointmentId,@RequestParam("id") Integer id) {
        try {
            return parkingLotsService.orderParking(appointmentId,id);
        } catch (Exception e) {
            log.error("orderParking has a error appointmentId={}", e, appointmentId);
            return Result.error("停车预约下单失败！");
        }
    }



    @GetMapping("/appointmentList")
    @Operation(summary = "我的预约列表")
    public Result<List<AppointmentListRes>> getAppointmentList(@RequestParam("id") Integer userid) {
        try {
            return parkingLotsService.getAppointmentList(userid);
        } catch (Exception e) {
            log.error("getAppointmentList has a error ", e);
            return Result.error("获取我预约列表失败！");
        }
    }

    @GetMapping("/fee/{appointId}")
    @Operation(summary = "获取费用")
    public Result<ParkingFeeRes> getFee(@PathVariable("appointId") int appointId) {
        try {
            return parkingLotsService.getFee(appointId);
        } catch (Exception e) {
            log.error("getFee has a error ", e);
            return Result.error("计算费用失败！");
        }
    }

    @GetMapping("/startUse")
    @Operation(summary = "开始使用")
    public Result<String> startUse(@RequestParam("appointmentId") int appointmentId) {
        try {
            return parkingLotsService.startUse(appointmentId);
        } catch (Exception e) {
            log.error("getAppointmentList has a error ", e);
            return Result.error("获取我预约列表失败！");
        }
    }


    @GetMapping("/leave/{appointId}")
    @Operation(summary = "离场")
    public Result<String> leaveParking(@PathVariable("appointId") int appointId) {
        try {
            return parkingLotsService.leaveParking(appointId);
        } catch (Exception e) {
            log.error("leave has a error ", e);
            return Result.error("离场失败！");
        }
    }

    @GetMapping("/fee/detail/{appointId}")
    @Operation(summary = "获取支付详情")
    public Result<ParkingNeedPayFeeRes> getFeeDetail(@PathVariable("appointId") int appointId) {
        try {
            return parkingLotsService.getFeeDetail(appointId);
        } catch (Exception e) {
            log.error("getFeeDetail has a error ", e);
            return Result.error("获取支付详情失败！");
        }
    }

    @GetMapping("/pay/fee/{parkingFeeId}")
    @Operation(summary = "进行支付")
    public Result<String> payParkingFee(@PathVariable("parkingFeeId") int parkingFeeId,@RequestParam("appointmentId")Integer appointmentId) {
        try {
            return parkingLotsService.payParkingFee(parkingFeeId,appointmentId);
        } catch (Exception e) {
            log.error("payParkingFee has a error ", e);
            return Result.error("支付失败！");
        }
    }

    @GetMapping("/admin/list")
    @Operation(summary = "管理员获取停车场列表")
    public Result<ParkingLotPageRes> getAdminParkingLot(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "type", defaultValue = "0") int type,
            @RequestParam(value = "title", defaultValue = "") String title
    ) {
        try {
            return parkingLotsService.getAdminParkingLot(pageNum, pageSize, type, title);
        } catch (Exception e) {
            log.error("getAdminParkingLot has a error", e);
            return Result.error("管理员获取停车场列表！");
        }
    }

    @GetMapping("/admin/deleted/{parkingLotId}")
    @Operation(summary = "管理员删除停车场")
    public Result<String> deletedParkLot(@PathVariable("parkingLotId") int parkingLotId) {
        try {
            return parkingLotsService.deletedParkLot(parkingLotId);
        } catch (Exception e) {
            log.error("deletedParkLot has a error", e);
            return Result.error("删除停车场失败！");
        }
    }

    @PostMapping("/admin/update/parkingLot")
    @Operation(summary = "管理员编辑停车场信息")
    public Result<String> updateParkingLot(@RequestBody ParkingLots parkingLots) {
        System.out.println(parkingLots);
        try {
            return parkingLotsService.updateParkingLot(parkingLots);
        } catch (Exception e) {
            log.error("updateParkingLot has a error", e);
            return Result.error("编辑失败！");
        }
    }
    @PostMapping("/admin/parkingLot/save")
    @Operation(summary = "添加停车场")
    public Result<String> saveparkingLots(@RequestBody ParkingLots parkingLots) {
        System.out.println("data="+parkingLots.toString());
        try {
            return parkingLotsService.saveparkingLot(parkingLots );
        } catch (Exception e) {
            log.error("saveBook has a error", e);
            return Result.error("获取书籍详情失败！");
        }
    }
}
